package com.martini.admin.controller;

import com.martini.admin.request.login.LoginReq;
import com.martini.common.domain.LoginInfo;
import com.martini.common.domain.R;
import com.martini.persistent.entity.SysUser;
import com.martini.persistent.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author martini at 2021/10/11 11:48
 */
@Slf4j
@RestController
@RequestMapping("/login")
public class LoginController {

    private final UserService userService;

    public LoginController(UserService userService) {
        this.userService = userService;
    }


    @PostMapping
    public R login(@RequestBody LoginReq loginReq) {
        SysUser waitLoginUser = new SysUser();
        BeanUtils.copyProperties(loginReq, waitLoginUser);
        LoginInfo info = userService.login(waitLoginUser);
        switch (info.getStatus()) {
            case LOGIN_OK:
                return R.success();
            case LOGIN_FAIL:
            default:
                return R.fail();
        }
    }
}
